var project={
    view:function(){
        return [
            m("div.page_title","系统清单"),
            m("div.page_menu",[
                m("button",{onclick:project_add.Show},"新增")
            ]),
            m("div.page_content.page_content2",m("table",{
                style:"table-layout:fixed;"
            },[
                m("tr",[
                    m("th","系统名称"),
                    m("th","项目路径"),
                    m("th","当前进度"),
                    m("th","操作")
                ]),
                project.data.list.map(item=>{
                    if(item.i_drop==1) return;
                    return m("tr",[
                        m("td",item.s_project_name),
                        m("td",item.s_project_path),
                        m("td",{
                            style:"color:"+project.color[item.s_status]+";"
                        },item.s_status),
                        m("td",[
                            m("button",{onclick:function(){
                                system.Show(item)
                            }},"查看文档"),
                            m("button",{onclick:function(){
                                project.Edit(item)
                            }},"更新"),
                            m("button",{onclick:function(){
                                project.Delete(item)
                            }},"删除")
                        ])
                    ])
                })
            ]))
        ]
    }
}
project.color={
    "规划":"#999999",
    "需求":"blue",
    "设计":"blue",
    "开发":"blue",
    "测试":"blue",
    "上线":"green",
    "退单":"silver"
};

project.data={page:1,pages:0,rows:0,op:[],list:[]};

project.Show=function(){
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/project.php?p=list"
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            project.data.list=res.list;
            m.render(document.querySelector(".content"),m(project))
        }else{
            msgbox.Error(res.message);
        }
    })
}

project.Edit=function(item){
    project_edit.data=item;
    project_edit.Show();
}

project.Delete=function(item){
    msgbox.Confirm("确定删除？");
    msgbox.callback=function(){
        var postData=new FormData();
        postData.append("id",item.id);
        msgbox.Loading();
        m.request({
            method:"POST",
            url:"php/project.php?p=delete",
            body:postData
        }).then(function(res){
            if(res.res=="OK"){
                msgbox.Hide();
                project.Show();
            }else{
                msgbox.Error(res.message);
            }
        })
    }
}

var project_add={
    view:function(){
        return [
            m("div.page_title","【新增】系统"),
            m("div.page_menu",[
                m("button",{onclick:project.Show},"返回")
            ]),
            m("div.page_content",[
                m("div.page_line",[
                    m("div.page_tag","* 系统名称"),
                    m("input[type=text].page_input#s_project_name")
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 系统路径"),
                    m("input[type=text].page_input#s_project_path")
                ])
            ]),
            m("div.page_foot",[
                m("button",{onclick:project_add.Submit},"保存")
            ])
        ]
    }
}

project_add.Show=function(){
    m.render(document.querySelector(".content"),m(project_add))
}

project_add.regex=[
    {"key":"s_project_name","regex":/^.{1,90}$/,"desc":"系统名称"},
    {"key":"s_project_path","regex":/^.{1,300}$/,"desc":"系统路径"},
];

project_add.GetPostData=function(){
    var postData=new FormData();
    for(var i=0;i<project_add.regex.length;i++){
        if(!document.querySelector("#"+project_add.regex[i].key)){
            continue;
        }
        if(!project_add.regex[i].regex.test(document.querySelector("#"+project_add.regex[i].key).value)){
            document.querySelector("#"+project_add.regex[i].key).style.backgroundColor="red";
            return false;
        }
        document.querySelector("#"+project_add.regex[i].key).style.backgroundColor="white";
        postData.append(project_add.regex[i].key,document.querySelector("#"+project_add.regex[i].key).value);
    }
    return postData;
}

project_add.Submit=function(){
    var postData=index.GetPostData(project_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    var _that=this;
    _that.disable=true;
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/project.php?p=add",
        body:postData
    }).then(function(res){
        _that.disable=false;
        msgbox.Hide();
        if(res.res=="OK"){
            project.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}

var project_edit={
    view:function(){
        return [
            m("div.page_title","【更新】系统"),
            m("div.page_menu",[
                m("button",{onclick:project.Show},"返回")
            ]),
            m("div.page_content",[
                m("div.page_line",[
                    m("div.page_tag","* 系统名称"),
                    m("input[type=text].page_input#s_project_name",{value:project_edit.data.s_project_name})
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 系统路径"),
                    m("input[type=text].page_input#s_project_path",{value:project_edit.data.s_project_path})
                ])
            ]),
            m("div.page_foot",[
                m("button",{onclick:project_edit.Submit},"保存")
            ])
        ]
    }
}
project_edit.data={};
project_edit.Show=function(){
    m.render(document.querySelector(".content"),m(project_edit))
}

project_edit.Submit=function(){
    var postData=index.GetPostData(project_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    postData.append("id",project_edit.data.id);
    var _that=this;
    _that.disable=true;
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/project.php?p=edit",
        body:postData
    }).then(function(res){
        _that.disable=false;
        msgbox.Hide();
        if(res.res=="OK"){
            project.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}